package group2assignone.ucm.customer;

import group2assignone.dao.MyDAO;
import group2assignone.model.Booking;
import group2assignone.model.Customer;
import group2assignone.model.Flight;
import group2assignone.ucm.UCController;
import group2assignone.ucm.UCStartup;
import group2assignone.view.UITool;
import group2assignone.view.customer.BookFlightView;
import group2assignone.view.customer.BookingConfirmedView;
import javax.swing.JOptionPane;

/**
 *Use Case Controller - allows the user to book a flight.
 * (Logged in or not)
 */
public class UCBookFlight implements UCController {

    private Flight flight;
    private Booking booking;
    private Customer customer;
    private BookFlightView view;
    private boolean booked;
    private int option;

    /**
     * assigns customer, flight and view
     * @param customer
     * @param flight
     * @param view
     */
    public UCBookFlight(Customer customer, Flight flight, BookFlightView view) {
        this.customer = customer;
        this.flight = flight;
        this.view = view;
        booking = new Booking();
    }

    /**
     * Use Case: Book Flight
     * Pre-Condition: User selects 'book' in book flight view.
     * Post-Condition: UI is updated
     * 
     * 1. Sytem verifies there are enough seats available.
     * 2. System adds requested tickets to booking.
     * 3. User confirms requested booking.
     * 4. System makes booking.
     * 5. System updates UI.
     *
     * 1.a. Not enough seats available.
     *     - System displays error message
     * 4.a. Booking fails
     *     - System displays error message
     */
    public void run() {
        
        MyDAO coms = UCStartup.getInstance().getDAO();

        //1. Sytem verifies there is enough seats available.
        if (view.getNumberTickets() <= coms.sQLGetSeatsAvailable(flight.getFlightNum())) {
            // 2. System adds requested tickets to booking.
            for (int i = 0; i < view.getNumberTickets(); i++) {
                booking.addFlight(flight.getFlightNum(), customer.getUsername());
            }

            option = view.confirmBook();
            if (option == JOptionPane.YES_OPTION) {//3. User confirms requested booking.
                //4. System makes booking.
                booked = booking.makeBooking();

                if (booked) { // 5. System updates UI.
                    BookingConfirmedView gui = new BookingConfirmedView(customer, flight, view.getNumberTickets(), booking.getBookingNum());
                    gui.go();
                    view.dispose();
                } else {//4.a. Booking fails
                        //- System displays error message
                    UITool.displayError("Booking Failed!!!!", view);
                }
            }
        } else {//1.a. Not enough seats available.
                //- System displays error message
            UITool.displayError("There are only " + coms.sQLGetSeatsAvailable(flight.getFlightNum()) + " seats available on this flight.", view);
        }
    }
}
